package com.zhong.thesimpleconnection.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhong.thesimpleconnection.R;

/**
 * Created by 俊峰 on 2017/5/16.
 */

public class MyDialogWarn extends Dialog {
    private MyDialogWarn(Context context) {
        super(context);
    }

    private MyDialogWarn(Context context, int themeResId) {
        super(context, themeResId);
    }

    private ImageView anim;
    private TextView title;
    private TextView content;
    private Button ok;
    private Button cancel;
    private Drawable mDrawable;
    private int state;
    private View.OnClickListener onDefaultClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cancel();
        }
    };
    private String titleString;
    private String contentString;
    private View.OnClickListener onPositiveListener = onDefaultClickListener;
    private View.OnClickListener onNegativeListener = onDefaultClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_warn);
        init();
    }

    @Override
    public void show() {
        super.show();
        show(this);
    }

    private void show(MyDialogWarn myDialogWarn){
        if (!TextUtils.isEmpty(myDialogWarn.titleString)) {
            myDialogWarn.title.setText(myDialogWarn.titleString);
        }
        if (!TextUtils.isEmpty(myDialogWarn.contentString)) {
            myDialogWarn.content.setText(myDialogWarn.contentString);
        }
        if (myDialogWarn.state == 1) {
            myDialogWarn.startWait();
        } else {
            myDialogWarn.stopWait();
        }

        myDialogWarn.ok.setOnClickListener(myDialogWarn.onPositiveListener);
        myDialogWarn.cancel.setOnClickListener(myDialogWarn.onNegativeListener);
    }

    private void init() {
        anim = (ImageView) findViewById(R.id.Dialog_Warn_Wait_Content);
        title = (TextView) findViewById(R.id.Dialog_Warn_Title);
        content = (TextView) findViewById(R.id.Dialog_Warn_Content);
        ok = (Button) findViewById(R.id.Dialog_Warn_OK);
        cancel = (Button) findViewById(R.id.Dialog_Warn_CANCLE);
    }

    private void startWait() {
        mDrawable = anim.getDrawable();
        if (mDrawable != null && mDrawable instanceof AnimationDrawable) {
            AnimationDrawable drawable = (AnimationDrawable) mDrawable;
            drawable.start();
            content.setVisibility(View.GONE);
        }
    }

    private void stopWait(){
        mDrawable = anim.getDrawable();
        if (mDrawable != null && mDrawable instanceof AnimationDrawable) {
            AnimationDrawable drawable = (AnimationDrawable) mDrawable;
            drawable.stop();
            anim.setVisibility(View.GONE);
            content.setVisibility(View.VISIBLE);
        }
    }

    public static class Builder{
        private MyDialogWarn mDialogWarn;
        public Builder(Context context){
            mDialogWarn = new MyDialogWarn(context);
        }
        public Builder setCancelable(boolean cancelable){
            mDialogWarn.setCancelable(cancelable);
            return this;
        }
        public Builder(Context context, int themeResId){
            mDialogWarn = new MyDialogWarn(context, themeResId);
        }
        public Builder setTitle(String sendTitle){
            mDialogWarn.titleString = sendTitle;
            return this;
        }
        public Builder setContent(String sendContent){
            mDialogWarn.contentString = sendContent;
            return this;
        }
        public Builder setState(int sendState){
            mDialogWarn.state = sendState;
            return this;
        }
        public Builder setPositiveButton(View.OnClickListener snedOnPosClickListener){
            mDialogWarn.onPositiveListener = snedOnPosClickListener;
            return this;
        }
        public Builder setNegativeButton(View.OnClickListener snedOnNegClickListener){
            mDialogWarn.onNegativeListener = snedOnNegClickListener;
            return this;
        }
        public MyDialogWarn create(){
            return mDialogWarn;
        }
    }
}
